/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-18 15:16:18
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.queue.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import cn.cmcc.online.smsapi.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.SysParamEntityManager;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.util.FileUtils;

public class ResourseUtil {

    public static final int RES_TYPE_LOGO = 1;// 企业LOGO

    /**
     * 通过类型检查是否需要check
     * 
     * @param resType
     * @return
     */
    public static boolean isNeedCheck(int resType) {
        try {
            int day = SysParamEntityManager.getIntParam(Constant.getContext(),
                    Constant.ONLINE_UPDATE_RES_PERIOD);
            if (day <= 0) {
                day = 2;
            }
            long time = SysParamEntityManager.getLongParam(
                    "LastCheckResourseTime_" + resType, 0,
                    Constant.getContext());
            if (System.currentTimeMillis() > (time
                    + DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_LOGO_RESOURSE_UPDATE_CYCLE,
                            Constant.jarsubFileUpdatePrio * day))) {
                return true;
            }
        } catch (Throwable e) {
            // TODO: handle Throwable
        }

        return false;
    }

    public static void updateCheckTime(int resType) {
        SysParamEntityManager.setParam("LastCheckResourseTime_" + resType,
                System.currentTimeMillis() + "");
    }

    /**
     * 删除历史的所有资源文件，临时文件的保存规则是 res_type_res_version_System.currentTimeMillis()
     * 只要版本小于或者等于当前版本的，都需要删除
     */
    public static void deleteTempZip(String res_type, String res_version) {
        // String dir = Constant.getPath("duoqu_temp");
        // List<File> list = FileUtils.getFileList(dir, res_type+"_", ".zip");
        List<File> list = getTempZip(res_type);
        if (list != null && !list.isEmpty()) {
            Iterator<File> it = list.iterator();
            while (it != null && it.hasNext()) {
                File file = it.next();
                if ((res_type + "_" + res_version + "_").compareTo(file
                        .getName()) >= 0) {
                    // 版本号比临时文件版本号大，删掉临时文件
                    file.delete();
                } else {
                    it.remove();
                }
            }
        }
    }

    /**
     * 获取未解压文件列表
     * 
     * @return
     */
    public static List<File> getTempZip(String res_type) {
        String dir = Constant.getPath("duoqu_temp");
        List<File> list = FileUtils.getFileList(dir, res_type + "_", ".zip");
        return list;
    }

    /* SDK-570 huangzhiqiang 20160802 start */
    public static String getXCode2() {
        return "3531333036463338";
    }
    /* SDK-570 huangzhiqiang 20160802 end */
}
